package com.shj.controller;

import com.shj.entity.SysRole;
import com.shj.service.SysRoleService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.awt.event.MouseEvent;
import java.util.List;

/**
 * @author songhuajian
 * @Description: TODO
 * @date 2020/5/9 21:51
 **/
@Controller("SysRoleController")
@RequestMapping("/system/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping("/find")
    public String find(Model model) {
        List<SysRole> list = sysRoleService.findAll();
        model.addAttribute("list", list);
        return "role/role_list";
    }

    @RequestMapping("/openAdd")
    public String openAdd(Model model) {
        return "role/role_add";
    }

    @RequestMapping("/add")
    public String add(Model model, SysRole sysRole, String fid) {
        String[] menus = null;
        if(fid != null && !"".equals(fid)){
            menus = fid.split(",");
        }
        sysRoleService.add(sysRole, menus);
        return  find(model);
    }

    @RequestMapping("/openUpdate")
    public String openUpdate(Model model, String roleId) {
        SysRole sysRole = sysRoleService.findByRoleId(roleId);
        model.addAttribute("role", sysRole);
        return "role/role_edit";
    }

    @RequestMapping("/edit")
    public String update(Model model, SysRole sysRole, String fid) {
        String[] menus = null;
        if(fid != null && !"".equals(fid)){
            menus = fid.split(",");
        }
        sysRoleService.update(sysRole, menus);
        return find(model);
    }

    @RequestMapping("/delete")
    public String delete(Model model, String roleId) {
        sysRoleService.delete(roleId);
        return find(model);
    }
}
